package com.xgj.ph.dao.impl;

import com.xgj.ph.dao.CourierDao;
import com.xgj.ph.entity.Courier;
import com.xgj.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierDaoImpl implements CourierDao {
  @Override
  public int insert(Courier courier) {
    String sql = "insert into courier values (null,?,?,?,?,?,?,?,?,?,?,?,?)";
    Object obj[] = {
            courier.getCourierStationName(),
            courier.getCourierStationImage(),
            courier.getCourierStationOrderCount(),
            courier.getCourierStationStatus(),
            courier.getAddTime(),
            courier.getCourierStationProvince(),
            courier.getCourierStationCity(),
            courier.getCourierStationDistrict(),
            courier.getCourierStationAddress(),
            courier.getCourierStationCommunityId(),
            courier.getCourierStationCommunityName(),
            courier.getCourierStationSort()
    };
    return JDBCDruidUtil.update(sql,obj);
  }
  
  @Override
  public int update(Courier courier) {
    String sql = "update courier set " +
            "courierStationName = ?," +
            "courierStationStatus = ?," +
            "courierStationProvince = ?," +
            "courierStationCity = ?," +
            "courierStationDistrict = ?," +
            "courierStationAddress = ?," +
            "courierStationCommunityId = ?" +
            " where courierStationId = ?";
    Object obj[] = {
            courier.getCourierStationName(),
            courier.getCourierStationStatus(),
            courier.getCourierStationProvince(),
            courier.getCourierStationCity(),
            courier.getCourierStationDistrict(),
            courier.getCourierStationAddress(),
            courier.getCourierStationCommunityId(),
            courier.getCourierStationId()
    };
    return JDBCDruidUtil.update(sql,obj);
  }
  
  @Override
  public int delete(int courierId) {
    String sql = "delete from courier where courierStationId = ?";
    Object obj[] = {courierId};
    return JDBCDruidUtil.update(sql,obj);
  }
  
  @Override
  public List<Courier> selectAll(Map<String, String> map) {
    List<Courier> list = new ArrayList<>();
    String sql = "select * from courier where 1=1";
    List<Object> params = new ArrayList<>();
    String courierStationName = map.get("courierStationName");
    if (courierStationName != null && !"".equals(courierStationName)){
      sql += " and courierStationName like ?";
      params.add("%"+courierStationName+"%");
    }
    String courierStationCommunityName = map.get("courierStationCommunityName");
    if (courierStationCommunityName != null && !"".equals(courierStationCommunityName)){
      sql += " and courierStationCommunityName like ?";
      params.add("%"+courierStationCommunityName+"%");
    }
    String startTime = map.get("startTime");
    if (startTime != null && !"".equals(startTime)){
      sql += " and addTime between ? and ?";
      params.add(startTime);
      params.add(map.get("endTime"));
    }
    String num = map.get("pageNum");
    if (num != null && !"".equals(num)){
      sql += " limit ?,?";
      int pageSize = Integer.parseInt(map.get("pageSize"));
      int pageNum = Integer.parseInt(num);
      params.add((pageNum-1)*pageSize);
      params.add(pageSize);
    }
    Object[] objects = params.toArray();
    ResultSet query = JDBCDruidUtil.query(sql, objects);
    while (true) {
      try {
        if (!query.next()) break;
        list.add(new Courier(
                query.getInt(1),
                query.getString(2),
                query.getString(3),
                query.getInt(4),
                query.getInt(5),
                query.getDate(6),
                query.getString(7),
                query.getString(8),
                query.getString(9),
                query.getString(10),
                query.getInt(11),
                query.getString(12),
                query.getInt(13)
        ));
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    JDBCDruidUtil.close();
    return list;
  }
  
  @Override
  public int selectCount(Map<String, String> map) {
    String sql = "select count(1) from courier where 1=1";
    ResultSet query = JDBCDruidUtil.query(sql, null);
    int count = 0;
    try {
      if (query.next()){
        count = query.getInt(1);
      }
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    JDBCDruidUtil.close();
    return count;
  }
}
